<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button id="btn">扫一扫</button>
    <script>
        // 暴露一个全局的函数方法
        window['_invoke_scan_callback_'] = function (result) {
            console.log(result)
        }
        var invokeScan = function () {
            var iframe = document.createElement('iframe');
            iframe.style.display = "none";
            // iframe.src="weixin://dl/scan"; 重要 调用扫一扫的接口方法
            // 传递参数和回调函数 类似于jsonp
            iframe.src = "weixin://dl/scan?k1=v1&callback=_invoke_scan_callback_";
            document.body.appendChild(iframe);
            // 使用异步销毁iframe  因为异步是等待同步代码执行完毕后再执行
            setTimeout(function () {
                document.body.removeChild(iframe);
                iframe = null;
            })
        }
        document.getElementById('btn').addEventListener('click', function () {
            invokeScan();
        })
    </script>
</body>

</html>